<template>
  <div>
    <img class="img1" src="static/img/login.jpg" alt />
    <div class="box">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm form"
      >
        <el-form-item label="用户名" prop="user">
          <el-input class="inp1" v-model="ruleForm.user"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input class="inp1" type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
        </el-form-item>
        <el-checkbox class="check" v-model="ruleForm.checked">记住密码</el-checkbox>
        <br />
        <br />
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// import Axios from 'axios';
import http from '../api/index';
// import axios from 'axios';
export default {
  data() {
    var validateUser = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };

    return {
      ruleForm: {
        user: '',
        pass: '',
        checked: false,
      },
      rules: {
        pass: [{ validator: validatePass, trigger: 'blur' }],
        user: [{ validator: validateUser, trigger: 'blur' }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          http
            .userLogin({
              administrators_name: this.ruleForm.user,
              administrators_password: this.ruleForm.pass,
            })
            .then((r) => {
              console.log(r);
              const res=r.data;
              const reg = r.code;
              console.log(res);
              if (reg !== 200) {
                this.$message({
                  showClose: true,
                  message: '登录失败',
                  type: 'error',
                });
              } else {
                this.$message({
                  showClose: true,
                  message: '登录成功',
                  type: 'success',
                });
                sessionStorage.setItem('token', res);
                // 页面跳转
                this.$router.push('/index');
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped>
.img1 {
  width: 100%;
  height: 605px;
  overflow: hidden;
}
.box {
  position: absolute;
  top: 158px;
  right: 200px;
  width: 400px;
  height: 300px;
  background-color: white;
}
.inp1 {
  width: 245px;
}
.form {
  margin-top: 40px;
}
.check {
  margin-left: 100px;
}
.btn2 {
  margin-left: 50px;
}
</style>
